package com.example.roomdemo;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import java.util.List;

/**
 * @author: tuyrt7 on 2020/9/4.
 */
public class StudentViewModel extends ViewModel {

    private StudentDao mStudentDao;
    private LiveData<List<Student>> listLiveData;

    public LiveData<List<Student>> getStudentList() {
        if (listLiveData == null) {
            mStudentDao = App.getMyDatabase().studentDao();
            listLiveData = mStudentDao.getAll();
        }
        return listLiveData;
    }

    public void insert(Student student) {
        new Thread(() -> {
            mStudentDao.insert(student);
        }).start();
    }

    public void update() {
        new Thread(() -> {
            Student st = mStudentDao.getStudentById(2);
            st.name = "updateName";
            mStudentDao.update(st);
        }).start();
    }

    public void delete(Student student) {
        new Thread(() -> {
            mStudentDao.delete(student);
        }).start();
    }
}
